import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:library_base/base/base_state_config.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/deliver_goods_entity.dart';
import 'package:library_base/ext/list_ext.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:library_base/res/dividing_line.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/route/routers.dart';
import 'package:library_base/utils/channel.dart';
import 'package:library_base/utils/date_util.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/dialog/dialog_util.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';
import 'package:library_base/base/base_list_state.dart';
import 'package:module_wms/page/deliver_goods/deliver_goods_controller.dart';
import 'package:module_wms/page/deliver_goods/deliver_goods_page_ui.dart';

class DeliverGoodsPage extends StatefulWidget {
  const DeliverGoodsPage({Key? key}) : super(key: key);

  @override
  _DeliverGoodsPageState createState() => _DeliverGoodsPageState();
}

class _DeliverGoodsPageState extends BaseListState<DeliverGoodsEntity, DeliverGoodsEntity, DeliverGoodsPage,
    DeliverGoodsController> {
  @override
  DeliverGoodsController registerController() => Get.put(DeliverGoodsController());

  MethodChannel fromAndroidChannel = const MethodChannel("DeliverGoodsPageFlutter");

  @override
  void onCreate() {
    super.onCreate();
    setNavigationBarVisible(false);
    setStatusBarVisible(false);
    fromAndroidChannel.setMethodCallHandler(flutterMethod);
  }

  showTimePicker() {
    var map = <String, dynamic>{};
    if (baseGetxController!.stationTimeBegin.isNotEmpty) {
      map['currentTime'] = DateTime.parse(baseGetxController!.stationTimeBegin);
    } else {
      map['currentTime'] = null;
    }
    map['filter'] = baseGetxController!.filter;
    map['reset'] = baseGetxController!.reset;
    DialogUtil.showTimePickDialog(arguments: map);
  }

  Future<dynamic> flutterMethod(MethodCall methodCall) async{
    switch (methodCall.method) {
      case TO_PRINTER_DETAIL_PAGE_CHANNEL:
        Get.toNamed(Routers.PRINT_DETAIL_PAGE,arguments: {"ids":baseGetxController!.stockOutIds});
        break;
    }
  }

  @override
  void controllerOnCreate() {
    useRefresh = true;
    isCanRefresh = true;
    isCanLoad = true;
    enableControlFinishLoad = true;
    enableControlFinishRefresh = true;
    baseGetxController?.easyRefreshController = controller;
  }

  @override
  Widget getNavBarRightWidget() {
    return filterWell(
        onTap: (stationTimeBegin, stationTimeEnd) {

        });
  }

  @override
  Widget? getMainWidget(BuildContext context) {
    return MultiStateWidget(
      emptyWidget: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            LocalImage('base_empty', package: Constant.packLib(), width: 0.6.sw),
            Text(MultiLanguage.noSuchProduct.tr, style: TextStyles.textSecondary28),
            SizedBox(height: 80.w),
            Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              GradientButton(
                ThemeUtil.getTheme(),
                width: 0.4.sw,
                height: 80.w,
                text: MultiLanguage.reload.tr,
                onPressed: baseGetxController!.tryReLoad,
              ),
              gapW24,
              GradientButton(
                ThemeUtil.getTheme(),
                width: 0.4.sw,
                height: 80.w,
                text: MultiLanguage.backToList.tr,
                onPressed: baseGetxController!.backToList,
              ),
            ])
          ]),
      controller: baseGetxController!.multiControllerOut,
      buildChild: (){
        return Scaffold(
          appBar: AppBar(
            leading: GestureDetector(
              onTap: (){
                Navigator.of(context).pop();
              },
              child: Icon(
                BaseUiConfig.globalBackIcon,
                color: BaseUiConfig.globalNavigationBarContentColor,
              ),
            ),
            elevation: 0.5,
            centerTitle: true,
            iconTheme: const IconThemeData(
              color: Colors.black, //修改颜色
            ),
            backgroundColor: Colors.white,
            title: Text(MultiLanguage.deliveryByWeight.tr,style: normalTextColor36,),
            actions: [
              PopupMenuButton<String>(
                onSelected: (value) {

                },
                itemBuilder: (context) {
                  return <PopupMenuEntry<String>>[
                    const PopupMenuItem<String>(
                      value: '语文',
                      child: Text('语文'),
                    ),
                    const PopupMenuItem<String>(
                      value: '数学',
                      child: Text('数学'),
                    ),
                    const PopupMenuItem<String>(
                      value: '英语',
                      height: 100,
                      child: Text('英语'),
                    ),
                  ];
                },
              )
            ],
          ),
          body: Stack(
            children: [
              Column(
                children: [
                  GetBuilder<DeliverGoodsController>(builder: (logic) {
                    return buildSearchWidget(baseGetxController!,onChanged: baseGetxController!.onChanged,onSearch: baseGetxController!.onSearch);
                  }),
                  Expanded(
                    child: Container(
                        color: ThemeUtil.getTheme().secondaryBgColor(),
                        child: getListView(context, baseGetxController)),
                  ),
                  // buildBottom(
                  //   baseGetxController!.finishedCount,
                  //   baseGetxController!.totalCount,
                  //   baseGetxController!.enable,
                  //   baseGetxController!.isSelectedAll,
                  //   baseGetxController!.stockOutIds.length,
                  //   deliverGoods:baseGetxController!.deliverGoods,
                  //   print:baseGetxController!.print,
                  // )
                ],
              ),
            ],
          ),
        );
      },
      child: null,
    );
  }

  @override
  List<Widget> buildListItem(BuildContext context) {
    return baseGetxController!.listData.value.buildItem((index, element) {
      return GetBuilder<DeliverGoodsController>(
        builder: (logic) {
          return buildItem(
            element,
            index,
            toOrderDetail: baseGetxController!.toOrderDetail,
          );
        },
      );
    }, dividingLine: () {
      return line2;
    });
  }


  @override
  bool get wantKeepAlive => true;
}
